package com.wn.main.ui.home.view

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.wn.common.constant.KEY_ID
import com.wn.framework.base.BaseFragment
import com.wn.framework.decoration.StaggeredItemDecoration
import com.wn.main.R
import com.wn.main.databinding.FragmentHomeVideoBinding
import com.wn.main.ui.home.adapter.HomeTabItemAdapter
import com.wn.main.ui.home.viewmodel.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2024/11/14 16:22
 * @Description: 首页资讯列表
 */
@AndroidEntryPoint
class HomeTabFragment : BaseFragment<FragmentHomeVideoBinding>(), OnRefreshLoadMoreListener {

  override fun getLayoutRes() = R.layout.fragment_home_video

  private val viewModel by viewModels<HomeViewModel>()

  // 不同的 Fragment 不同的 Adapter 实例
  @Inject
  lateinit var mAdapter: HomeTabItemAdapter

  @Inject
  lateinit var itemDecoration: StaggeredItemDecoration

  // 分页加载数据
  private var mPage = 1

  // 实例化的 Fragment id ,该 id 由 数据提取
  private var mId: Int? = null

  companion object {
    // 实例化 Fragment 且 通过 arguments 传递 id
    fun newInstance(id: Int): HomeTabFragment = HomeTabFragment().apply {
      arguments = Bundle().apply { putInt(KEY_ID, id) }
    }
  }

  override fun initView() {
    super.initView()
    mBinding.apply {
      refreshLayout.apply {
        autoRefresh()
        setOnRefreshListener(this@HomeTabFragment)
        setOnLoadMoreListener(this@HomeTabFragment)
      }

      recyclerView.apply {
        val manager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        layoutManager = manager
        addItemDecoration(itemDecoration)
        adapter = mAdapter
      }
    }
  }

  override fun initData() {
    super.initData()
    // 获取传递 的  id
    mId = arguments?.getInt(KEY_ID)
  }

/*  override fun initLazyData() {
    mBinding.refreshLayout.apply {
      autoRefresh()
      setOnRefreshListener(this@HomeTabFragment)
      setOnLoadMoreListener(this@HomeTabFragment)
    }
  }*/

  /**
   * 获取项目列表数据
   * 刷新 mPage = 1
   * 加载 mPage ++
   */
  private var tabJob: Job? = null
  private fun getData() {
    tabJob?.cancel()
    tabJob = lifecycleScope.launch {
      lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
        viewModel.getProjectList(mPage, mId ?: 0).collect {
          if (mPage == 1) {
            // 加载更多 最后没有数据 得到的就是 null 所里 这里 骨架屏只针对首次刷新 即 mPage == 1
            mBinding.viewEmptyData.isVisible = it?.datas.isNullOrEmpty()
            it ?: return@collect
            mAdapter.setData(it.datas)
            mBinding.refreshLayout.finishRefresh()
          } else {
            mAdapter.addData(it?.datas)
            mBinding.refreshLayout.finishLoadMore()
          }
        }
      }
    }
  }

  override fun onRefresh(refreshLayout: RefreshLayout) {
    mPage = 1
    getData()
  }

  override fun onLoadMore(refreshLayout: RefreshLayout) {
    mPage++
    getData()
  }

  /**
   * Home键或者锁屏 后 数据重复请求
   */
  override fun onStop() {
    super.onStop()
    tabJob?.cancel()
  }
}